package com.youbug.mall.product_pms.service.impl;

import com.youbug.mall.product_pms.entity.PmsSkuImages;
import com.youbug.mall.product_pms.mapper.PmsSkuImagesMapper;
import com.youbug.mall.product_pms.service.IPmsSkuImagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * sku图片 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-04-21
 */
@Service
public class PmsSkuImagesServiceImpl extends ServiceImpl<PmsSkuImagesMapper, PmsSkuImages> implements IPmsSkuImagesService {

        @Override
        public IPage<PmsSkuImages> getPmsSkuImagesPage(QueryVo<PmsSkuImages> queryVo) {

        Page<PmsSkuImages> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<PmsSkuImages> wrapper = new QueryWrapper<>();

        PmsSkuImages pmsSkuImages = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(pmsSkuImages)){
            if(StringUtils.hasText(pmsSkuImages.getImgUrl())){
                wrapper.eq("img_url", pmsSkuImages.getImgUrl());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<PmsSkuImages> page = this.page(paramPage, wrapper);

        return page;
        }

    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public boolean saveBatch(Collection<PmsSkuImages> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    @Override
    public List<PmsSkuImages> getImagesBySkuId(Long skuId) {
        List<PmsSkuImages> skuImages = this.list(new QueryWrapper<PmsSkuImages>().eq("sku_id", skuId));

        return skuImages;
    }

}
